package com.acme.bankapp;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.exception.BadStringToParseClient;
import com.acme.bankapp.domain.bank.exception.ConnectionException;
import com.acme.bankapp.domain.bank.transport.ClientStub;
import com.acme.bankapp.domain.bank.transport.NetworkConnection;
import com.acme.bankapp.service.bank.BankDataLoaderService;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class BankClient {

    private static final String host = "127.0.0.1";
    private static final int port = 7777;

    public static void main(String[] args) {


        try (Socket socket = new Socket(host, port);
             NetworkConnection nc = new NetworkConnection(socket);
             Scanner s = new Scanner(System.in)
        ) {
            ClientStub cs = new ClientStub(nc);
            while (true) {
                String command = s.nextLine();
                System.out.println(command);
                switch (command) {
                    case "ADD":
                        try {
                            System.out.println("try to create Client: " + cs.addClient(BankDataLoaderService.parseString(s.nextLine())));
                        } catch (BadStringToParseClient badStringToParseClient) {
                            System.out.println("bad client!");
                        }
                        break;
                    case "STAT":
                        for (Client c : cs.getClients()) {
                            System.out.println(c);
                        }
                        break;
                    case "Q":
                        System.out.println("Exit!");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ConnectionException ce) {
            System.out.println("Connection error! " + ce.getCause());
        }
    }
}
